﻿using BLL.Dal;
using BLL.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SysAdmin_Template_News_List : System.Web.UI.Page
{
    public string news_code = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Request["news_code"] != null && Request["news_code"] != "")
            {
                news_code = Request["news_code"];
            }
            else
            {
                WebHelper.HttpResponseJson("你正在违规访问页面！");
            }
        }

        if (Request["paramType"] != null && Request["paramType"] != "")
        {
            switch (Request["paramType"])
            {
                case "QueryList": QueryList(); break;
                case "AddModel": AddModel(); break;
                case "DelModel": DelModel(); break;
                case "DelAtt": DelAtt(); break;
                //case "QueryModel": QueryModel(); break;
                case "UpdateModel": UpdateModel(); break;
                default: break;

            }
        }

    }

    /// <summary>
    /// 查询加载项
    /// </summary>
    void QueryList()
    {
        int PageCount = 0;

        if (Request["page"] != null)
        {
            int.TryParse(Request["page"], out PageCount);
        }

        int RowsCount = 30;

        if (Request["rows"] != null)
        {
            int.TryParse(Request["rows"], out RowsCount);
        }

        CommNewsDAL newsBus = new CommNewsDAL();
        int recordCount = newsBus.GetRecordCount("");

        DataTable dtData = newsBus.GetListByPage(Request["news_code"], (RowsCount * (PageCount - 1)), RowsCount * PageCount).Tables[0];

        //数据格式化处理
        for (int i = 0; i < dtData.Rows.Count; i++)
        {

            if (dtData.Rows[i]["CreateTime"] != null && dtData.Rows[i]["CreateTime"].ToString() != "")
            {
                dtData.Rows[i]["temp_createtime"] = Convert.ToDateTime(dtData.Rows[i]["CreateTime"]).ToString("yyyy-MM-dd");
            }
            else
            {
                dtData.Rows[i]["temp_createtime"] = "";
            }
        }


        object obj = new { total = recordCount, rows = dtData };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);

    }

    /// <summary>
    /// 增加新的项
    /// </summary>
    void AddModel()
    {
        CommNewsDAL modelBus = new CommNewsDAL();
        CommNews model = new CommNews();
        model.NewsKey = ReqHelper.Get<string>("news_key");
        model.Title = ReqHelper.Get<string>("newsTitle");
        model.Content = ReqHelper.Get<string>("site_model_news_add_textarea");
        model.NewSource = ReqHelper.Get<string>("newsSource");
        model.Author = ReqHelper.Get<string>("newsAuthor");
        model.KeyWord = ReqHelper.Get<string>("newsKeyWord");
        model.ImageUrl = "";
        model.NewsType = ReqHelper.Get<string>("news_type");
        model.CreateTime = DateTime.Now;
        model.UpdateTime = DateTime.Now;

        object obj = new { success = true, msg = "新闻增加成功" };
        try
        {
            modelBus.Add(model);
            obj = new { success = true, msg = "新闻增加成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 编辑项
    /// </summary>
    void UpdateModel()
    {
        string strNewsKey = "";
        strNewsKey = Request["news_key"].ToString();
        CommNewsDAL modelBus = new CommNewsDAL();
        CommNews model = modelBus.GetModel(strNewsKey);
        model.Title = ReqHelper.Get<string>("newsTitle");
        model.Content = ReqHelper.Get<string>("site_model_news_edit_textarea");
        model.NewSource = ReqHelper.Get<string>("newsSource");
        model.Author = ReqHelper.Get<string>("newsAuthor");
        model.KeyWord = ReqHelper.Get<string>("newsKeyWord");
        model.ImageUrl = "";
        model.NewsType = ReqHelper.Get<string>("news_type");
        model.UpdateTime = DateTime.Now;

        object obj = new { success = true, msg = "新闻编辑成功" };
        try
        {
            modelBus.Update(model);
            obj = new { success = true, msg = "新闻编辑成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 删除项
    /// </summary>
    void DelModel()
    {
        string strCode = ReqHelper.Get<string>("news_key");
        CommNewsDAL modelBus = new CommNewsDAL();
        object obj = new { success = modelBus.Delete(strCode) };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);

    }

    /// <summary>
    /// 删除附件
    /// </summary>
    void DelAtt()
    {
        string strCode = ReqHelper.Get<string>("att_key");
        AttachmentDAL modelBus = new AttachmentDAL();
        object obj = new { success = modelBus.Delete(new Guid(strCode)) };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }
}